const app = getApp(),
  $extConfig = xhs.getExtConfigSync(),
  $config = require("../../config.js"),
  WxParse = require("../wxParse/wxParse.js"),
  $appId = $extConfig.appId || $config.appId,
  $host = $extConfig.host || $config.host,
  //业务域名
  $resHost = $extConfig.resHost || $config.resHost,
  appId = $extConfig.appId || $config.appId; //资源域名
Component({
  /**
   * 组件的属性列表a
   */
  properties: {
    authoriz: {
      type: Boolean,
      value: false
    },
    isQrcode: {
      type: Boolean,
      value: false
    },
    isSqdl: {
      //用户登录授权登录协议弹出
      type: Boolean,
      value: false
    },
    isPhone: {
      //手机授权
      type: Boolean,
      value: false
    },
    isOpenPhone: {
      //主动触发手机授权
      type: Boolean,
      value: false
    }
  },
  attached() {
    this.getPrivacySetting();
    app.login().then(() => {
      // app._getData('/user/user/extend-data').then((data) => {
      //   if (data.errCode == 0) {
      //     if(data.data.focusAuth && data.data.needAuth == '1'){//需要强制授权登录
      //       this.setData({
      //         isFocusAuth: true
      //       })
      //     }
      //     try {
      //       var value = wx.getStorageSync('isMandatoryPhone')
      //       this.setData({
      //         isMandatoryPhone: value
      //       })
      //     } catch (e) {
      //       // Do something when catch error
      //     }
      //     //判断是否需要手机号码授权
      //     let { mobile = '' } = app.$getUser();
      //     if (mobile) {
      //       this.setData({
      //         userMobile: mobile
      //       })
      //       this.updateUserMobile(mobile)
      //     }
      //     //先这样。以后再优化吧。。
      //     this.setData({
      //       isShowInfo:(this.data.isSqdl || (!this.data.authoriz && this.data.isQrcode) || this.data.isFocusAuth || (this.data.isMandatoryPhone == '1' && !this.data.hasValidate) || this.data.isOpenPhone) && ((!this.data.authoriz || this.data.isFocusAuth) || (!this.data.isPhone && this.data.isSqdl))
      //     })
      //   } else {
      //     wx.showToast({
      //       title: data.errInfo,
      //       icon: 'none',
      //       mask: true,
      //       duration: 3000
      //     });
      //   }
      // })
    });
    app.$getConfigData();
    this.setData({
      logo: typeof data.logo == "object" ? xhs.getStorageSync("config").wechatInfo.logo.url : this.data.defImg,
      wechatInfo: xhs.getStorageSync("config").wechatInfo,
      userAgreement: xhs.getStorageSync("config").wechatInfo.userAgreement
    });
    let article = xhs.getStorageSync("config").wechatInfo.userAgreement;
    if (article) {
      WxParse.wxParse("article", "html", article, this, 5);
    }
    this.setData({
      authoriz: xhs.getStorageSync("authoriz")
    });
  },
  data: {
    focus: false,
    wechatInfo: {},
    defImg: `${$resHost}/img/def.jpg`,
    isFocusAuth: false,
    isMandatoryPhone: "0",
    //是否强制授权手机
    hasValidate: false,
    userMobile: "",
    userAgreement: "",
    items: [{
      checked: "true",
      value: true
    }],
    isTrue: true,
    isShowXy: false,
    isShowInfo: false,
    needAuthorization: false,
    //用户隐私协议
    privacyContractName: "" //隐私协议的名称
  },
  /**
   * 组件的方法列表
   */
  methods: {
    updateUserMobile(validateMobile) {
      this.setData({
        validateMobile,
        hasValidate: true
      });
      if (!this.data.userMobile) {
        let user = app.$getUser();
        user.mobile = validateMobile;
        xhs.setStorage({
          key: "user",
          data: user
        });
      }
    },
    checkboxChange(e) {
      this.setData({
        isTrue: e.detail.value.length > 0 ? e.detail.value[0] : false
      });
    },
    closeFn() {
      this.setData({
        isQrcode: false,
        isOpenPhone: false
      });
    },
    closeSq() {
      this.setData({
        isSqdl: false,
        isFocusAuth: false
      });
    },
    setInfo() {
      this.setData({
        isSqdl: false,
        isFocusAuth: false
      });
      // 获取加载的页面对象
      let pages = getCurrentPages();
      // 获取当前页面的对象
      let currentPage = pages[pages.length - 1];
      // 当前页面url
      let url = currentPage.route;
      if (url == "pages/TabBar/Home/me") {
        currentPage.getRefreshFn();
      } else {
        if (currentPage.setUpdate) setTimeout(function () {
          currentPage.setUpdate();
        }, 500);
      }
    },
    okSq() {
      this.setData({
        isSqdl: false,
        isFocusAuth: false
      });
      // this.triggerEvent('oksq');
    },
    getMobile(e) {
      this.triggerEvent("okphone", e);
    },
    validateFn() {
      this.setData({
        hasValidate: true,
        isOpenPhone: false
      });
      app.isEmployeeFn();
      // 获取加载的页面对象
      let pages = getCurrentPages();
      // 获取当前页面的对象
      let currentPage = pages[pages.length - 1];
      // 当前页面url
      let url = currentPage.route;
      if (url == "pages/TabBar/Home/me") {
        currentPage.getRefreshFn();
      } else if (url == "pages/TabBar/Home/home") {
        currentPage.getRefreshFn();
      } else {
        if (currentPage.setUpdate) setTimeout(function () {
          currentPage.setUpdate();
        }, 500);
      }
    },
    closeX() {
      this.setData({
        isShowXy: false
      });
    },
    openXy() {
      this.setData({
        isShowXy: true
      });
    },
    getUserInfo() {
      this.setData({
        isShowInfo: true
      });
      //console.log(this.data.isShowInfo)
    },
    openPrivacyContract() {
      xhs.openPrivacyContract({
        success: res => {
          console.log("openPrivacyContract success");
        },
        fail: res => {
          console.error("openPrivacyContract fail", res);
        }
      });
    },
    handleAgreePrivacyAuthorization() {
      this.setData({
        needAuthorization: false,
        focus: true
        // },()=>{
        //   this.getUserInfo()
      });
    },
    getPrivacySetting() {
      if (xhs.getPrivacySetting) {
        xhs.getPrivacySetting({
          success: res => {
            this.setData({
              needAuthorization: res.needAuthorization
            });
            if (res.needAuthorization) {
              this.setData({
                privacyContractName: res.privacyContractName
              });
            } else {
              this.setData({
                focus: true
              });
              this.triggerEvent("agree");
            }
          },
          fail: () => {},
          complete: () => {}
        });
      } else {
        // 低版本基础库不支持 wx.getPrivacySetting 接口，隐私接口可以直接调用
        this.triggerEvent("agree");
        this.setData({
          focus: true
        });
      }
    }
  }
});